﻿using Dapper;
using DapperExtensions;
using Logistics.Model;
using Logistics_Dao;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.Service
{
    public class BidBaseService
    {
        BidBaseDao dao = new BidBaseDao();
        Bid_CloseDao closeDao = new Bid_CloseDao();
        CityBaseDao citydao = new CityBaseDao();
        CompanyDao compaydao = new CompanyDao();
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int BidBaseAdd(wuliu_bid_base model)
        {
            return dao.BidBaseAdd(model);
        }

        public int  GetBidBaseListCount(int companyid,int state)
        {
            return dao.GetBidBaseListCount(companyid, state);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int BidBaseUpdate(wuliu_bid_base model)
        {
            return dao.BidBaseUpdate(model);
        }

        public PageData<DataTable> GetQueryBidList(string number,  int times, int pageIndex, int pageSize)
        {
            return dao.GetContractList(number,  times, pageIndex, pageSize);
        }

        public List<wuliu_bid_base> GetBidList(int companyid)
        {
            return dao.GetBidBaseList(companyid);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bid_id"></param>
        /// <param name="del_flag"></param>
        /// <returns></returns>
         public int UpdateDelFlag(int bid_id, int del_flag)
        {
            return dao.UpdateDelFlag(bid_id, del_flag);
        }

        public object GetBidInfo(int bid_id)
         {
             return dao.GetBidInfo(bid_id);
         }


        public List<wuliu_bid_base> GetAutoStartBidList(int num)
        {
            return dao.GetAutoStartBidList(num);
        }

          public List<wuliu_bid_base> GetAutoEndBidList(int num)
        {
            return dao.GetAutoEndBidList(num);
        }

        /// <summary>
        /// 获取所有标单数据
        /// </summary>
        /// <returns></returns>
          public int GetUpdateBidBaseList()
          {
              var list= dao.GetUpdateBidBaseList();
              foreach(var mod in list)
              {
                  wuliu_bid_base enty = new wuliu_bid_base();
                  enty.bid_id = mod.bid_id;
                  List<string> cityidx = new List<string>();
                  List<string> cityname = new List<string>();
                  if(mod.end_city_id!=null)
                  {
                      int strcityid = (int)mod.end_city_id;
                      cityidx = GetCityBaseID(strcityid, cityidx);
                      enty.end_city_id_x = string.Join("|", cityidx.ToArray());
                      enty.end_city_name = string.Join("|", GetCityName(strcityid, cityname).ToArray());
                  }
                  if (mod.company_id!=null)
                  {
                      enty.company_name = compaydao.GetCompanyById((int)mod.company_id).company_name;
                  }

                  dao.BidBaseUpdate(enty);
              }
              return list.Count();
          }

          public List<string> GetCityBaseID(int cityid, List<string> cityidx)
          {
              var modcity = citydao.GetCityBase(cityid);
              if (modcity != null)
              {
                  if (modcity.city_id > 0)
                  {
                      cityidx.Insert(0,modcity.city_id.ToString());
                      int sub_city_id = (int)modcity.sub_city_id;
                      if (sub_city_id == 86)
                      {
                          return cityidx;
                      }
                      GetCityBaseID(sub_city_id, cityidx);
                  }
              }
              return cityidx;
          }
          public List<string> GetCityName(int cityid, List<string> cityname)
          {
              var modcity = citydao.GetCityBase(cityid);
              if (modcity != null)
              {
                  if (modcity.city_id > 0)
                  {
                      cityname.Insert(0, modcity.city_name.ToString());
                      int sub_city_id = (int)modcity.sub_city_id;
                      if (sub_city_id == 86)
                      {
                          return cityname;
                      }
                      GetCityName(sub_city_id, cityname);
                  }
              }
              return cityname;
          }

        public int DeleteBid(int company_id, int bid_id)
        {
            //TODO Log Operte Log
            return dao.DeleteBid(company_id, bid_id);
        }

        public wuliu_bid_base GetBid(int bid_id)
        {
            return dao.GetBid(bid_id);
        }

        /// <summary>
        /// 获取手机标单详情
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public object GetPhoneBidInfo(int bid_id)
        {
            return dao.GetPhoneBidInfo(bid_id);
        }

        public PageModel GetPhoneBidList(IPredicate predicate, int pageIndex, int pageSize)
        {
            return dao.GetPhoneBidList(predicate, pageIndex, pageSize);
        }

        public PageData<wuliu_bid_base> GetBidList(object predicate, int pageIndex, int pageSize)
        {
            return dao.GetBidList(predicate, pageIndex, pageSize);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bid_close"></param>
        /// <returns></returns>
        public bool ScrapBid(wuliu_bid_close bid_close)
        {
            var bid_base = dao.GetBid((int)bid_close.bid_id);
            if (bid_base != null && bid_base.del_flag == "1")
            {
                closeDao.Add(bid_close);
                bid_base.state = 140;
                bid_base.del_flag = "10";
                bid_base.del_remark = bid_close.reason_info;
                return dao.ScrapBid(bid_base);
            }
            return false;
        }
    }
}
